<template>
  <div>
    <h2>添加医院信息</h2>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="医院名称" prop="hosname">
        <el-input v-model="ruleForm.hosname"></el-input>
      </el-form-item>
      <el-form-item label="医院编号" prop="hoscode">
        <el-input v-model="ruleForm.hoscode"></el-input>
      </el-form-item>
      <el-form-item label="api基础路劲" prop="apiUrl">
        <el-input v-model="ruleForm.apiUrl"></el-input>
      </el-form-item>
      <el-form-item label="联系人姓名" prop="contactsName">
        <el-input v-model="ruleForm.contactsName"></el-input>
      </el-form-item>
      <el-form-item label="联系人手机" prop="contactsPhone">
        <el-input v-model="ruleForm.contactsPhone"></el-input>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">修改信息</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button @click="goLists()">返回医院信息列表</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import hp from '@/api/hospital';

export default {
  data() {
    return {
      ruleForm: {
        hosname: '',
        hoscode: '',
        apiUrl: '',
        contactsName: '',
        contactsPhone: ''

      },
      rules: {
        hosname: [
          {required: true, message: '请输入活动名称', trigger: 'blur'},
          {min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur'}
        ],
        contactsPhone: [
          {required: true, message: '请输入联系人电话', trigger: 'blur'},
          {min: 11, max: 11, message: '电话长度为11位', trigger: 'blur'}
        ],
        hoscode: [
          {required: true, message: '请输入医院编号', trigger: 'change'}
        ],
        apiUrl: [
          {required: true, message: '请输入api基础路劲', trigger: 'change'}
        ],
        contactsName: [
          {required: true, message: '请输入联系人姓名', trigger: 'change'}
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // console.log(this.ruleForm)

          // alert('submit!');
          this.$confirm('是否修改医院信息, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            hp.HospUpdate(this.ruleForm).then(response => {
              console.log(response)
              if (response.data) {
                this.$message({
                  type: 'success',
                  message: '修改成功!'
                });
                this.$router.push('/admin/lists')
              } else {
                this.$message({
                  type: 'error',
                  message: '修改失败!'
                });
              }

            })

          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消修改'
            });
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    goLists() {
      this.$router.push('/admin/lists')
    }

  },
  created() {
    // alert("进入add")
    if (typeof this.$route.query.row != typeof undefined) {
      // console.log(this.$route.query.row);
      this.ruleForm = this.$route.query.row
    } else {
      this.$router.push('admin/lists')
    }

  }
}
</script>

<style>

</style>
